#include "LineChart.h"
#include "ui_LineChart.h"

LineChart::LineChart(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LineChart)
{
    ui->setupUi(this);
    initUI();
    HandleHelper::GetInstance()->_send2Chart.connect(boost::bind(&LineChart::flushData, this, _1));

}
static qint64 MSecBase;
LineChart::~LineChart()
{
    delete ui;
}

void LineChart::initUI()
{
    chart = new QChart;

    chart->legend()->show();

    series1=new QSplineSeries();

    series2=new QSplineSeries();

    series3=new QSplineSeries();


    series1->setName("X/a");
    series2->setName("Y/b");
    series3->setName("Z/c");

    chart->addSeries(series1);
    chart->addSeries(series2);
    chart->addSeries(series3);

    chart->createDefaultAxes();

    QValueAxis    *axisY_1=new QValueAxis();

    axisY_1->setMin(0);

    axisY_1->setMax(1024);

    /// 设置坐标轴上的格点

    axisY_1->setTickCount(10);


    axisY_1->setTitleText("xyz-abc");


    /// 设置坐标轴的颜色，粗细，设置网格不显示
    axisY_1->setLinePenColor(QColor(Qt::darkBlue));

    axisY_1->setGridLineColor(QColor(Qt::darkBlue));

    axisY_1->setGridLineVisible(false);


    QPen penY1(Qt::darkBlue,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);

    axisY_1->setLinePen(penY1);

    /// 把坐标轴添加到chart中，
    /// addAxis函数的第二个参数是设置坐标轴的位置，
    /// 只有四个选项，下方：Qt::AlignBottom，左边：Qt::AlignLeft，右边：Qt::AlignRight，上方：Qt::AlignTop


    chart->addAxis(axisY_1,Qt::AlignLeft);

    series1->attachAxis(axisY_1);

    series1->setUseOpenGL(true);
    series2->setUseOpenGL(true);
    series3->setUseOpenGL(true);

    MSecBase = QDateTime::currentDateTime().toMSecsSinceEpoch();
    ui->graphicsView->setChart(chart);

}

#define X_WIDTH 15
//实现画线函数，动态更新
void LineChart::flushData(double* it)
{
    float currentS =(QDateTime::currentDateTime().toMSecsSinceEpoch() - MSecBase)/1000.0;

    chart->axisX()->setRange(currentS-15 , currentS);

    if(series1->count() < 600){
        series1->append(QPointF(currentS,it[0]));

        series2->append(QPointF(currentS,it[1]));

        series3->append(QPointF(currentS,it[2]));
    }



}

void LineChart::on_btn_clear_clicked()
{
    series1->clear();
    series2->clear();
    series3->clear();


    if(series3->count() > 600){
        series3->removePoints(0,series3->count()/2);
    }
}

void LineChart::on_comboBox_currentIndexChanged(int index)
{
    QChart::ChartTheme theme = static_cast<QChart::ChartTheme>(index);

    if (chart->theme() != theme) {
        chart->setTheme(theme);
        QPalette pal = window()->palette();
        if (theme == QChart::ChartThemeLight) {
            pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else if (theme == QChart::ChartThemeDark) {
            pal.setColor(QPalette::Window, QRgb(0x121218));
            pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
        } else if (theme == QChart::ChartThemeBlueCerulean) {
            pal.setColor(QPalette::Window, QRgb(0x40434a));
            pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
        } else if (theme == QChart::ChartThemeBrownSand) {
            pal.setColor(QPalette::Window, QRgb(0x9e8965));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else if (theme == QChart::ChartThemeBlueNcs) {
            pal.setColor(QPalette::Window, QRgb(0x018bba));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else if (theme == QChart::ChartThemeHighContrast) {
            pal.setColor(QPalette::Window, QRgb(0xffab03));
            pal.setColor(QPalette::WindowText, QRgb(0x181818));
        } else if (theme == QChart::ChartThemeBlueIcy) {
            pal.setColor(QPalette::Window, QRgb(0xcee7f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        } else {
            pal.setColor(QPalette::Window, QRgb(0xf0f0f0));
            pal.setColor(QPalette::WindowText, QRgb(0x404044));
        }
        window()->setPalette(pal);
    }
}

void LineChart::on_next_clicked()
{
    emit updateChart(1);
}
